package com.dhnsoft;

public class 最长公共前缀 {
    public static void main(String[] args) {
        String[] strs = {"flower","flow","flight"};
        String s = longestCommonPrefix(strs);
        System.out.println("ddd:"+s);
    }

    public static String longestCommonPrefix(String[] strs) {
        //首先随便获取一个数组值
        String s = strs[0];
        for (String str : strs) {
            //遍历一下所有数组的值和初始的值是否有相同前缀，如果没有则退出
            while(!str.startsWith(s)){
                if(s.length()==0)return "";
                //如果有相同前缀，但是不知道有多少位，就慢慢递减
                s = s.substring(0,s.length()-1);
            }
        }
        return s;
    }
}
